package org.ligson.httptest.loginallvalue;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.ligson.httptest.util.HttpUtil;
import org.ligson.httptest.util.LoginUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

@Slf4j
@Data
public class LoginAllValue implements Callable<Boolean> {
    private String userName;
    private String password;
    private String baseUrl;

    @Override
    public Boolean call() throws Exception {
        boolean isOk = false;
        String token = LoginUtil.getAccessToken(baseUrl, userName, password);
        if (StringUtils.isNoneBlank(token)) {
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", "Bearer " + token);
            String result = HttpUtil.doGet(baseUrl + "/crux-sm/api/usercontext/allvalue", headers);
            if (StringUtils.isNoneBlank(result)) {
                isOk = true;
            }
            result = HttpUtil.doGet(baseUrl + "/crux-appmodule/api/appfunction?size=999999999", headers);

            if (StringUtils.isNoneBlank(result)) {
                isOk = true && isOk;
            } else {
                isOk = false;
            }
        }
        if (isOk) {
            log.debug("用户:{}测试通过", userName);
        } else {
            log.error("用户:{}测试未通过(test fail)", userName);
        }
        return isOk;
    }
}
